package com.zzu.base.controller.role;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzu.base.common.pojo.ResultResponse;
import com.zzu.base.controller.permission.vo.PermissionGetRespVO;
import com.zzu.base.controller.role.vo.RoleAddReqVO;
import com.zzu.base.controller.role.vo.RoleGetRespVO;
import com.zzu.base.controller.role.vo.RolePageReqVO;
import com.zzu.base.controller.role.vo.RoleUpdateReqVO;
import com.zzu.base.entity.RoleDO;
import com.zzu.base.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/role")
@Tag(name = "角色")
@PreAuthorize("hasPermission(null,'restricted_use')")
public class RoleController {
    @Resource
    private RoleService roleService;

    private static final CopyOptions COPY_OPTIONS = CopyOptions.create()
            .setIgnoreNullValue(false)
            .setIgnoreError(true);

    @Operation(method = "角色权限添加")
    @GetMapping("/permission/add")
    @PreAuthorize("hasPermission(null,'permit_all')")
    public ResultResponse<Boolean> rolePermissionAdd(@Positive Long permissionId, @Positive Long roleId) {
        return ResultResponse.success(roleService.addRolePermission(roleId, permissionId));
    }

    @Operation(method = "角色权限移除")
    @GetMapping("/permission/remove")
    @PreAuthorize("hasPermission(null,'permit_all')")
    public ResultResponse<Boolean> rolePermissionRemove(@Positive Long permissionId, @Positive Long roleId) {
        return ResultResponse.success(roleService.removeRolePermission(roleId, permissionId));
    }

    @Operation(method = "角色权限列表")
    @GetMapping("/permission/list")
    public ResultResponse<List<PermissionGetRespVO>> rolePermissionList(@Positive Long id) {
        return ResultResponse.success(roleService.getPermissionsByRoleId(id).stream()
                .map(permissionDO -> BeanUtil.toBean(permissionDO, PermissionGetRespVO.class, COPY_OPTIONS))
                .collect(Collectors.toList()));
    }

    @Operation(method = "角色获取")
    @GetMapping("/get")
    public ResultResponse<RoleGetRespVO> roleGet(@Positive Long id) {
        RoleDO roleDO = roleService.getById(id);
        return ResultResponse.success(BeanUtil.toBean(roleDO, RoleGetRespVO.class, COPY_OPTIONS));
    }

    @Operation(method = "角色列表")
    @GetMapping("/list")
    public ResultResponse<List<RoleGetRespVO>> roleList() {
        List<RoleDO> roleList = roleService.list();
        return ResultResponse.success(roleList.stream()
                .map(roleDO -> BeanUtil.toBean(roleDO, RoleGetRespVO.class, COPY_OPTIONS))
                .collect(Collectors.toList()));
    }

    @Operation(method = "角色新增")
    @PostMapping("/add")
    @PreAuthorize("hasPermission(null,'permit_all')")
    public ResultResponse<Boolean> roleAdd(@Valid @RequestBody RoleAddReqVO roleAddReqVO) {
        RoleDO roleDO = BeanUtil.toBean(roleAddReqVO, RoleDO.class, COPY_OPTIONS);
        return ResultResponse.success(roleService.save(roleDO));
    }

    @Operation(method = "角色修改")
    @PostMapping("/update")
    @PreAuthorize("hasPermission(null,'permit_all')")
    public ResultResponse<Boolean> roleUpdate(@Valid @RequestBody RoleUpdateReqVO roleUpdateReqVO) {
        RoleDO roleDO = BeanUtil.toBean(roleUpdateReqVO, RoleDO.class, COPY_OPTIONS);
        return ResultResponse.success(roleService.update(roleDO));
    }

    @Operation(method = "角色移除")
    @GetMapping("/remove")
    @PreAuthorize("hasPermission(null,'permit_all')")
    public ResultResponse<Boolean> roleRemove(@Positive Long id) {
        return ResultResponse.success(roleService.removeById(id));
    }

    @Operation(method = "角色分页")
    @PostMapping("/page")
    public ResultResponse<IPage<RoleGetRespVO>> rolePage(@Valid @RequestBody RolePageReqVO rolePageReqVO) {
        IPage<RoleDO> rolePage = roleService.getRolePage(rolePageReqVO);

        IPage<RoleGetRespVO> resultPage = new Page<>();
        BeanUtil.copyProperties(rolePage, resultPage, COPY_OPTIONS);
        List<RoleGetRespVO> records = rolePage.getRecords().stream()
                .map(roleDO -> BeanUtil.toBean(roleDO, RoleGetRespVO.class, COPY_OPTIONS))
                .collect(Collectors.toList());
        resultPage.setRecords(records);

        return ResultResponse.success(resultPage);
    }
}